#include "../include/Configuration.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <unistd.h>
// 静态成员初始化
std::unique_ptr<Configuration> Configuration::_instance = nullptr;

Configuration *Configuration::getInstance()
{
    if (_instance == nullptr)
    {
        _instance = std::unique_ptr<Configuration>(new Configuration());
    }
    return _instance.get();
}

void Configuration::load(const std::string &filename)
{
    // 使用绝对路径
    std::string full_path = "/home/jingcheng/practice/7_SearchEngine/search_engine-v2/config/server.conf";

    std::cout << "🔍 加载配置文件: " << full_path << std::endl;

    std::ifstream file(full_path);
    if (!file.is_open())
    {
        std::cerr << "❌ 错误: 无法打开配置文件 " << full_path << std::endl;
        std::cerr << "请检查文件是否存在且路径正确" << std::endl;
        return;
    }

    std::cout << "✅ 成功打开配置文件!" << std::endl;

    std::string line;
    int line_num = 0;
    int loaded_count = 0;

    while (std::getline(file, line))
    {
        line_num++;

        // 跳过空行和注释
        if (line.empty() || line[0] == '#')
        {
            continue;
        }

        // 查找等号分隔符
        size_t pos = line.find('=');
        if (pos == std::string::npos)
        {
            std::cerr << "警告: 第 " << line_num << " 行格式错误(缺少=): " << line << std::endl;
            continue;
        }

        // 提取key和value
        std::string key = line.substr(0, pos);
        std::string value = line.substr(pos + 1);

        // 去除首尾空格
        key.erase(0, key.find_first_not_of(" \t"));
        key.erase(key.find_last_not_of(" \t") + 1);
        value.erase(0, value.find_first_not_of(" \t"));
        value.erase(value.find_last_not_of(" \t") + 1);

        if (key.empty())
        {
            continue;
        }

        // 存储配置项
        _config[key] = value;
        loaded_count++;
        std::cout << "  加载配置: " << key << " = " << value << std::endl;
    }

    file.close();
    std::cout << "✅ 配置文件加载完成，共 " << loaded_count << " 个配置项" << std::endl;

    // 验证关键配置项是否加载成功
    std::cout << "📋 配置验证:" << std::endl;
    std::cout << "  server.port: " << get("server.port", "未找到") << std::endl;
    std::cout << "  server.thread_pool_size: " << get("server.thread_pool_size", "未找到") << std::endl;
    std::cout << "  search.max_suggestions: " << get("search.max_suggestions", "未找到") << std::endl;
}

std::string Configuration::get(const std::string &key, const std::string &defaultValue)
{
    auto it = _config.find(key);
    if (it != _config.end())
    {
        return it->second;
    }

    // std::cout << "警告: 配置项 '" << key << "' 不存在，使用默认值: " << defaultValue << std::endl;
    return defaultValue;
}

int Configuration::getInt(const std::string &key, int defaultValue)
{
    std::string value = get(key, "");
    if (value.empty())
    {
        return defaultValue;
    }

    try
    {
        return std::stoi(value);
    }
    catch (const std::exception &e)
    {
        std::cerr << "错误: 配置项 '" << key << "' 的值 '" << value << "' 不是有效的整数" << std::endl;
        return defaultValue;
    }
}